package com.hellenic.servlets;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.SessionFactory;

import com.hellenic.mod.ReservationMod;

@SuppressWarnings( "serial" )
public class FormReservation extends HttpServlet {
    private static final String ATTR_DAO_FACTORY  = "sessionF";
    private static final String ATTR_ID_PROPRIETE = "idPropriete";
    private static final String VUE               = "/WEB-INF/reservation.jsp";

    private ReservationMod         reservationM;

    public void init() throws ServletException {
        // Récupération de l'objet SessionFactory
        SessionFactory sf = (SessionFactory) getServletContext().getAttribute( ATTR_DAO_FACTORY );
        // intanciation de la classe metier
        reservationM = new ReservationMod( sf );
        System.out.println( "Message : Initialisation servlet FormReservation" );
    }

    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        // recupération de l'idPropriete
        String idPropriete = request.getParameter( ATTR_ID_PROPRIETE );
        request.setAttribute( ATTR_ID_PROPRIETE, idPropriete );
        // appel du form
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    }

    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        // enregistrement de la reservation
        HashMap<String, String> resultForm = reservationM.createResa( request );
        // stockage des messages dans l'obj request
        request.setAttribute( "resultForm", resultForm );
        request.setAttribute( ATTR_ID_PROPRIETE, resultForm.get( "idPropriete" ) );
        // passage a la jsp
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    }
}
